package cn.huiyunche.bms.web.controller;

import cn.huiyunche.bms.backend.bo.AuditBO;
import cn.huiyunche.bms.backend.query.ReceiptAduitWithOrderConditions;
import cn.huiyunche.bms.backend.service.AuditService;
import cn.huiyunche.bms.web.vo.AuditVO;
import cn.huiyunche.bms.web.vo.BmsReceiptAuditVO;
import cn.huiyunche.commons.domain.PageVo;
import cn.huiyunche.commons.domain.ResultDTO;
import cn.huiyunche.commons.exception.BusinessException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;

/**
 * Created by Felix on 2017/8/18 9527.
 */

@Controller
@RequestMapping("/audit")
public class AuditController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuditController.class);

    @Autowired
    private AuditService auditService;
    /**
     * 审核列表
     *
     * @param pageVo
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/audit-list", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> listByPage(PageVo pageVo, BmsReceiptAuditVO vo) throws Exception {
        LOGGER.info("AuditController.listByPage params : {}, {}", pageVo, vo);
        ResultDTO<Object> result = new ResultDTO<>(true, null, "查询成功");
        try {
            ReceiptAduitWithOrderConditions conditions = new ReceiptAduitWithOrderConditions();
            BeanUtils.copyProperties(vo, conditions);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            if (vo.getBillDateStartString() != null)
                conditions.setCreateTimeStart(format.parse(vo.getBillDateStartString()));
            if (vo.getBillDateEndString() != null)
                conditions.setCreateTimeEnd(format.parse(vo.getBillDateEndString()));
            result.setData(auditService.selectReceiptAuditByConditions(pageVo, conditions));
        } catch (Exception e) {
            result.setSuccess(false);
            LOGGER.error("AuditController.listByPage error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Object> detail(AuditVO vo) {
        ResultDTO<Object> result = new ResultDTO<>(true, null, "查询成功");
        AuditBO bo = new AuditBO();
        BeanUtils.copyProperties(vo, bo);
        try {
            result.setData(auditService.detail(bo));
        } catch (Exception e) {
            LOGGER.error("AuditController detail error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     * 审核状态
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> updateStatus(BmsReceiptAuditVO vo) throws Exception {
        LOGGER.info("AuditController.updateStatus params : {}, {}", vo);
        ResultDTO<Object> result = new ResultDTO<>(true, null, "审核成功");
        try {
            ReceiptAduitWithOrderConditions conditions = new ReceiptAduitWithOrderConditions();
            BeanUtils.copyProperties(vo, conditions);
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            String username = (String)session.getAttribute("username");
            conditions.setAuditUser(username);
            result.setData(auditService.updateStatus(conditions));
        } catch (Exception e) {
            result.setSuccess(false);
            LOGGER.error("AuditController.updateStatus error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }


    //跳转审核详情页面
    @RequestMapping("comment")
    public String comment(HttpServletResponse response, Model model, HttpServletRequest request) throws Exception {
        response.setHeader("X-Frame-Options", "SAMEORIGIN");// 解决IFrame拒绝的问题
        return "audit/comment";
    }
}
